home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C & C++ Multimedia Cyber Classroom
/
C and C++ Multimedia Cyber Classroom (Prentice Hall) (1998).iso
/
cpphtp2
/
cpphtp2.jar
/
chpt_12.gml
< prev
next >
Wrap
Text File
|
1998-03-03
|
59KB
|
1,487 lines
<html>
<chapter>
<section type=Popup name=Terminology title="Terminology">
<page>
<font size=14>
A<br>
angle brackets (<b><</b> and <b>></b>)
<a href="%s2p1"><img src=iicons/bullbib.gif></a>
<br>
C<br>
class template
<a href="%s1p1"><img src=iicons/bullbib.gif></a>
<a href="%s4p0"><img src=iicons/bullbib.gif></a>
<br>
class template header
<a href="%s4p5"><img src=iicons/bullbib.gif></a>
<br>
F<br>
formal parameter in a
function template
definition
<a href="%s2p4"><img src=iicons/bullbib.gif></a>
<br>
<b>friend</b>
<a href="%s1p2"><img src=iicons/bullbib.gif></a>
<a href="%s7p0"><img src=iicons/bullbib.gif></a>
<a href="%s7p1"><img src=iicons/bullbib.gif></a>
<br>
function template
<a href="%s1p0"><img src=iicons/bullbib.gif></a>
<a href="%s2p0"><img src=iicons/bullbib.gif></a>
<a href="%s3p0"><img src=iicons/bullbib.gif></a>
<a href="%s4p7"><img src=iicons/bullbib.gif></a>
<br>
K<br>
</font>
</page>
<page>
<font size=14>
keyword <b>class</b>
<a href="%s2p2"><img src=iicons/bullbib.gif></a>
<br>
keyword <b>template</b>
<a href="%s2p1"><img src=iicons/bullbib.gif></a>
<br>
N<br>
non-type parameter
<a href="%s5p0"><img src=iicons/bullbib.gif></a>
<br>
O<br>
overloading template
functions
<a href="%s3p0"><img src=iicons/bullbib.gif></a>
<br>
P<br>
parameterized type
<a href="%s4p1"><img src=iicons/bullbib.gif></a>
<a href=""><img src=iicons/bullbib.gif></a>
<br>
S<br>
<b>static</b>
<a href="%s1p2"><img src=iicons/bullbib.gif></a>
<br>
<b>static</b> data member
<a href="%s8p0"><img src=iicons/bullbib.gif></a>
<br>
T<br>
</font>
</page>
<page>
<font size=14>
<b>template</b>
<a href="%s2p1"><img src=iicons/bullbib.gif></a>
<br>
template class
<a href="^Perform::c:s0p0"><img src=iicons/bullbib.gif></a>
<br>
template function
<a href="^Perform::c:s0p0"><img src=iicons/bullbib.gif></a>
<br>
type parameter
<a href="%s2p3"><img src=iicons/bullbib.gif></a>
<a href="%s4p2"><img src=iicons/bullbib.gif></a>
<a href="%s5p0"><img src=iicons/bullbib.gif></a>
<br>
<b>typename</b>
<a href="^Errors::c:s0p0"><img src=iicons/bullbib.gif></a>
<br>
<br>
</font>
</page>
</section>
<section type=Popup name=Quotes title="Quotes">
<page>
<i>Behind that outside
pattern
the dim shapes get clearer
every day.
It is always the same
shape, only very
numerous.</i> <br>
Charlotte Perkins Gilman<br>
<br>
</page>
<page>
<i>If you are able to slip
through the parameters
of the skies and the earth,
then do so.</i> <br>
The Koran<br>
<br>
</page>
<page>
<i>A Mighty Maze! but not
without a plan.</i> <br>
Alexander Pope<br>
<br>
</page>
</section>
<section type=Popup name=Illustration title="Illustrations">
<page>
<a href="^Code::c:s0p0">Fig. 12.1</a> A function template.<br>
<a href="^Code::c:s0p1">Fig. 12.2</a> Using template functions.<br>
<a href="^Code::c:s0p2">Fig. 12.3</a> Demonstrating class template <b>Stack</b>.<br>
<a href="^Code::c:s0p3">Fig. 12.4</a> Passing a <b>Stack</b> template object to a function template.<br>
<br>
</page>
</section>
<section type=Popup name=Answers title="Answers">
<page pagename="Answer 12.1">
<b>Answer 12.1</b><br>
a) False. It could be a non-template function.<br>
b) False. Each template class will have a copy of the <b>static</b> data member.<br>
c) True.<br>
d) False. Formal parameter names among template functions need not be
unique.<br>
e) False. Keyword <b>class</b> in this context also allows for a type parameter of a built-
in type.<br>
<foreign name="exercises" url="^Exercises::c:s0p0">
<br>
</page>
<page pagename="Answer 12.2">
<b>Answer 12.2</b><br>
a) template functions, template classes.<br>
b) <b>template</b>, angle brackets (<b><</b> and <b>></b>).<br>
c) overloading.<br>
d) parameterized.<br>
e) binary scope resolution.<br>
f) file.<br>
<foreign name="exercises" url="^Exercises::c:s0p2">
<br>
</page>
<page pagename="Answer 12.9">
<b>Answer 12.9</b><br>
A function template is used to instantiate template functions.<br>
<foreign name="exercises" url="^Exercises::c:s0p10">
<br>
</page>
<page pagename="Answer 12.14">
<b>Answer 12.14</b><br>
If the compiler cannot match the function call made to a template or if the
matching process results in multiple matches at compile time, the compiler
generates an error.<br>
<foreign name="exercises" url="^Exercises::c:s0p15">
<br>
</page>
<page pagename="Answer 12.15">
<b>Answer 12.15</b><br>
When creating template classes from a class template, it is necessary to provide a
type (or possibly several types) to complete the definition of the new type being
declared. For example, when creating an "array of integers" from an <tt><b>Array</b></tt> class
template, the type <tt><b>int</b></tt> is provided to the class template to complete the
definition of an array of integers.<br>
<foreign name="exercises" url="^Exercises::c:s0p16">
<br>
</page>
<page pagename="Answer 12.19">
<b>Answer 12.19</b><br>
To specify at compile time the size of the container class object being declared.<br>
<foreign name="exercises" url="^Exercises::c:s0p20">
<br>
</page>
<page pagename="Answer 12.23">
<b>Answer 12.23</b><br>
For <tt><b>static</b></tt> members of a class template, each template class instantiated
receives its own copy of all the <tt><b>static</b></tt> members. Then all objects instantiated
for a given template class access that particular template class's <tt><b>static</b></tt>
members.<br>
<foreign name="exercises" url="^Exercises::c:s0p25">
<br>
</page>
<page pagename="Answer 12.3">
<b>Answer 12.3</b><br>
The solution to this exercise can be found on your Cyber Classroom CD. Copy
the file cpphtp2/answers/P12_03.zip to your hard drive and unzip the program
code.<br>
<foreign name="exercises" url="^Exercises::c:s0p4">
<br>
</page>
<page pagename="Answer 12.7">
<b>Answer 12.7</b><br>
The solution to this exercise can be found on your Cyber Classroom CD. Copy
the file cpphtp2/answers/P12_07.zip to your hard drive and unzip the program
code.<br>
<foreign name="exercises" url="^Exercises::c:s0p8">
<br>
</page>
</section>
<section type=Popup name=Exercises title="Exercises">
<page pagename="Exercise 12.1">
<b>Exercise 12.1</b><br>
Answer each of the following true or false. For those that are false, state why.<br>
a) A <b>friend</b> function of a function template must be a template function.<br>
b) If several template classes are generated from a single class template with a
single <b>static</b> data member, each of the template classes shares a single copy of
the class template's <b>static</b> data member.<br>
c) A template function can be overloaded by another template function with the
same function name.<br>
d) The name of a formal parameter can be used only once in the formal
parameter list of the template definition. Formal parameter names among
template definitions must be unique.<br>
<foreign name="answers" url="^Answers::c:s0p0">
</page>
<page pagename="Exercise 12.1">
e) Keyword <b>class</b> as used with a template type parameter specifically means
"any user-defined class type."<br>
<foreign name="answers" url="^Answers::c:s0p0">
<br>
</page>
<page pagename="Exercise 12.2">
<b>Exercise 12.2</b><br>
Fill in the blanks in each of the following:<br>
a) Templates enable us to specify, with a single code segment, an entire range of
related functions called ________ , or an entire range of related classes called
______.<br>
b) All function template definitions begin with the keyword ________
followed by a list of formal parameters to the function template enclosed in
________.<br>
c) The related functions generated from a function template all have the same
name, so the compiler uses ________ resolution to invoke the proper function.<br>
d) Class templates are also called ________ types.<br>
<foreign name="answers" url="^Answers::c:s0p1">
</page>
<page pagename="Ex